//
//  TTRecyclingSendTopView.m
//  TaoTuo
//
//  Created by LG on 2020/8/15.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingSendTopView.h"

@interface TTRecyclingSendTopView ()
{
    NSArray *hintArray;
}

@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UIButton *infoBtn;
@property (nonatomic, copy) void(^selectItemBlock)(NSInteger index);

@end

@implementation TTRecyclingSendTopView

- (instancetype)initWithFrame:(CGRect)frame selectItemBlock:(void(^)(NSInteger index))selectItemBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = UIColor.whiteColor;
        hintArray = @[@" 无需寄送货品，直接上架售卖。", @" 请选择顺丰、EMS进行邮寄。", @" 请先线下与我们的业务员取得联系。", @" 请先线下与我们的业务员取得联系。"];
        self.selectItemBlock = selectItemBlock;
        
        [self setupSubviews];
        
    }
    return self;
}

- (void)btnClick:(UIButton *)sender
{
    sender.selected = YES;
    self.selectBtn.selected = NO;
    self.selectBtn = sender;
    
    NSString *hintStr = hintArray[sender.tag-123];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:hintStr];
    if ([hintStr containsString:@"顺丰、EMS"]) {
        [attr addAttribute:NSForegroundColorAttributeName value:UIColorHex(#DE4747) range:[hintStr rangeOfString:@"顺丰、EMS"]];
    }
    [self.infoBtn setAttributedTitle:attr forState:UIControlStateNormal];
    
    
    if (self.selectItemBlock) {
        self.selectItemBlock(sender.tag - 123);
    }
    
    
}

- (void)setupSubviews
{
    UILabel *titleLabel = [UILabel labelWithTitle:@"寄货方式" color:UIColorHex(#333333) font:[UIFont boldSystemFontOfSize:15]];
    titleLabel.frame = CGRectMake(15, 5, 200, 30);
    [self addSubview:titleLabel];
    
    NSArray *titles = @[@"免寄上架", @"自助邮寄", @"上门取货", @"到店交易"];
    NSArray *images = @[@"mianjishangjia", @"mail", @"shangmen", @"daodian"];
    CGFloat space = 10;
    CGFloat w = (self.width - 30 - (titles.count - 1) * space) / titles.count;
//    CGFloat space = (self.width - 30 - w * titles.count) / (titles.count - 1);
    for (int i = 0; i < titles.count; i++) {
        CGFloat x = 15 + (w + space) * i;
        UIButton *btn = [UIButton createButtonWithFrame:CGRectMake(x, titleLabel.bottom+5, w, 68) image:[UIImage imageNamed:images[i]] title:titles[i] titleColor:UIColorHex(#333333) titleFont:[UIFont systemFontOfSize:12] backgroundColor:nil target:self action:@selector(btnClick:)];
        [btn setTitleColor:UIColorHex(#9748EC) forState:UIControlStateSelected];
        NSString *selName = [NSString stringWithFormat:@"%@_sel", images[i]];
        [btn setImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage imageWithColor:UIColorHex(#F7F7F7)] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:UIColorHex(#F9F4FF)] forState:UIControlStateSelected];
        btn.tag = 123 + i;
        [self addSubview:btn];
        
        if (i == 0) {
            btn.selected = YES;
            self.selectBtn = btn;
        }
        
    }
    
    _infoBtn = [UIButton createButtonWithTitle:@"" titleColor:UIColorHex(#343434) titleFont:[UIFont systemFontOfSize:12] backgroundColor:nil target:nil action:nil];
    [_infoBtn setImage:[UIImage imageNamed:@"hintgantanhao"] forState:UIControlStateNormal];
    _infoBtn.frame = CGRectMake(15, 110, self.width - 25, 20);
    _infoBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self addSubview:_infoBtn];
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:hintArray.firstObject];
    [attr addAttribute:NSForegroundColorAttributeName value:UIColorHex(#DE4747) range:[hintArray.firstObject rangeOfString:@"顺丰、EMS"]];
    [self.infoBtn setAttributedTitle:attr forState:UIControlStateNormal];
    
}

@end
